/*
package com.ruiyi.subway.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

*/
/**
 * Created by zhangruiqian on 16/10/28.
 *//*

@Service
public class UserManagerService {
    @Autowired
    private MailService mailService;
    @Autowired
    private UserService userService;
    @Autowired
    private OrgService orgService;


    private String propertiesFile = "vcs-client.properties";
    private String clientHost;
    private final String userListHtml;
    private final String applyLinkHtml;

    //TODO 后续看是否需要配置, 目前3600
    private static Long expireTime = 36000 * 1000L;

    public UserManagerService() {
        Properties properties = PropertiesUtil.loadClasspathProperties(propertiesFile);
        this.clientHost = properties.getProperty("client.host", "http://localhost:8080");
        this.userListHtml = properties.getProperty("user-list-html", "/vcs/sa/resources/users.html");
        this.applyLinkHtml = properties.getProperty("apply-link-html", "/vcs/sa/resources/apply-link.html");
    }

    public void approveUser(String userName, String orgId, String key) throws VcsBusinessException {
        try {
            String mail = authKey(key);
            User user = userService.findByID(userName);
            if (user == null || user.isValid() || !mail.equals(user.getApplyMail())) {
                throw new VcsBusinessException("您无法授权该用户");
            }
            user.setOrgId(orgId);
            user.setValid(true);
            userService.update(user);
        } catch (VcsEncryptException e) {
            throw new VcsBusinessException(e.getMessage(), e);
        }
    }

    public void addReviewAuthToUser(String userName, String authId) throws VcsBusinessException {
        User user = userService.findByID(userName);
        if (user == null || !user.isValid() || user.getTenantId() == null) {
            throw new VcsBusinessException("您无法授权该用户");

        }
        if (user.getReviewAuthorityID() != null && user.getReviewAuthorityID().contains(authId)) {
            throw new VcsBusinessException("用户已被授权，请不要重复授权");
        }

        user.addReviewAuthorityID(authId);
        user.setRoleName(RoleType.REVIEW);
        userService.update(user);
    }

    public void deleteReviewAuthToUser(String userName, String authId) throws VcsBusinessException {
            User user = userService.findByID(userName);
            if (user == null || !user.isValid() || user.getTenantId() == null) {
                throw new VcsBusinessException("用户状态异常");
            }
            List<String> reviewAuthority = user.getReviewAuthorityID();
            reviewAuthority.remove(authId);
            user.setReviewAuthorityID(reviewAuthority);
            if (reviewAuthority.size() == 0) {
                user.setRoleName(RoleType.NORMAL);
            }
            userService.update(user);
    }


    private String authKey(String key) throws VcsBusinessException, VcsEncryptException {
        ManagerToken token = null;
        token = decryptToken(key);
        if ((System.currentTimeMillis() - expireTime) > token.getEncryptTime()) {
            throw new VcsBusinessException("授权码已超时，请重新申请");
        }
        return token.getManagerEmail();
    }

    private ManagerToken decryptToken(String key) throws VcsEncryptException {
        String content = EncryptUtil.decryptAES(key);
        return GsonUtil.parsePojo(content, ManagerToken.class);
    }


    public void deleteUser(String userName, String key) throws VcsBusinessException {
        try {
            String mail = authKey(key);
            User user = userService.findByID(userName);
            //TODO 忽略大小写
            if (user == null || !mail.equals(user.getApplyMail())) {
                throw new VcsBusinessException("您无法删除该用户");
            }
            userService.delete(userName);
        } catch (VcsEncryptException e) {
            throw new VcsBusinessException(e.getMessage(), e);
        }
    }

    public List<User> getManagedUsers(String key, int ps, int p) throws VcsBusinessException {
        try {
            String mail = authKey(key);
            return userService.selectByApplyMail(mail, new QueryPage(p, ps));
        } catch (VcsEncryptException e) {
            e.printStackTrace();
            throw new VcsBusinessException(e.getMessage(), e);
        }
    }

    public void sendManagerMail(String managerMail) throws VcsBusinessException {
        //TODO 邮箱地址校验
        try {
            String key = userService.generateManagerKey(managerMail);
            //userList地址
            String userListLink = this.clientHost + this.userListHtml + "?key=" + key;
            //申请link地址
            String requestLink = this.clientHost + this.applyLinkHtml;
            //TODO 修改邮件内容
            String content = String.format("您申请的管理地址链接如下，如非本人操作，请忽略该邮件" +
                            "<a href='%s'>用户管理</a></br>" +
                            "该链接1个小时有效，如需重新生成链接，请点击重新生成" +
                            "</br>",
                    userListLink);
            String subject = "双录系统用户管理链接";
            mailService.sendSmtpMail(managerMail, subject, content);
        } catch (Exception e) {
            e.printStackTrace();
            throw new VcsBusinessException("操作失败", e);
        }

    }

    public List<OrgDto> getManagedUserOrgs(String key) throws VcsBusinessException {
        try {
            String mail = authKey(key);
            VcsTenant tenant = userService.findTenantByMailSuffix(userService.getMailAddressSuffix(mail));
            return orgService.selectByTenant(tenant.getId());
        } catch (VcsEncryptException e) {
            e.printStackTrace();
            throw new VcsBusinessException(e.getMessage(), e);
        }
    }

    public void initAdmin(String userName, String userPassword, String userCnName) throws VcsBusinessException {
        User user = new User();
        user.setName(userName);
        user.setUserPass(userPassword);
        user.setCnName(userCnName);
        user.setRoleName(RoleType.ADMIN);
        user.setValid(true);
        userService.save(user);
    }
}
*/
